package com.ichifinga.airwave.shared.entities;

import com.ichifinga.airwave.shared.enums.DisplaySpeeds;
import com.ichifinga.airwave.shared.enums.FlashTypes;
import com.ichifinga.airwave.shared.enums.ScrollTypes;
import com.ichifinga.airwave.shared.enums.ShadeTypes;
import com.ichifinga.airwave.shared.enums.TextShades;
import com.ichifinga.airwave.shared.enums.TextSizes;
import com.ichifinga.airwave.shared.enums.TextWidths;
import com.ichifinga.airwave.shared.enums.Colours;
import android.content.Context;
import android.content.SharedPreferences;

public class Phrase
{
	public int _phraseId;
	public String _textToDisplay;
	public TextSizes _textSize; 			//Small, Medium, Large
	public TextWidths _textWidth;			//Thin, Normal, Thick
	public TextShades _textShade;			//OneColour, TwoColour, ThreeColour, Rainbow, Chill, Fire, Ice
	public Colours _colourOne;				//White, Red, Orange, Yellow,
	public Colours _colourTwo;				//...Green, Blue, Indigo,Violet, 
	public Colours _colourThree;			//...Cyan, Magenta, Pink
	public Colours _backgroundColour;
	public ShadeTypes _shadeType;			//Gradient, Block
	public DisplaySpeeds _displaySpeed;	//Slow, Normal, Fast
	public ScrollTypes _scrollType;		//Scroll, Bounce, Fit
	public FlashTypes _flashType;	
	
	public Phrase()
	{
		
	}
	
	//generic phrase
	
	private static void SetPhraseId(Context context, int currentPhraseId, boolean editMode)
	{
		SharedPreferences sharedPreferences;
    	sharedPreferences = context.getSharedPreferences("SharedPreferences", Context.MODE_WORLD_WRITEABLE);
    	SharedPreferences.Editor editor = sharedPreferences.edit();
    	
    	if(editMode)
    	{
    		editor.putInt("EditPhraseId", currentPhraseId);
    	}
    	else
    	{
    		editor.putInt("CurrentPhraseId", currentPhraseId);
    	}
		
		editor.commit();
	}

	private static int GetPhraseId(Context context, boolean editMode)
	{
		SharedPreferences sharedPreferences;
    	sharedPreferences = context.getSharedPreferences("SharedPreferences", Context.MODE_WORLD_READABLE);
    	    	
    	if(editMode)
    	{
    		return sharedPreferences.getInt("EditPhraseId", 1);
    	}
    	else
    	{
    		return sharedPreferences.getInt("CurrentPhraseId", 1);
    	}
	}


	
	//current phrase
	
	public static void SetCurrentPhraseId(Context context, int phraseId)
	{
		SetPhraseId(context, phraseId, false);
	}
	
	private static int GetCurrentPhraseId(Context context)
	{
		return GetPhraseId(context, false);
	}
	
	public static Phrase GetCurrentPhrase(Context context)
	{
		return Phrase.GetPhrase(context, Phrase.GetCurrentPhraseId(context));
	}
	
	//edit phrase
	
	public static void SetEditPhraseId(Context context, int editPhraseId)
	{
		SetPhraseId(context, editPhraseId, true);
	}
	
	private static int GetEditPhraseId(Context context)
	{
		return GetPhraseId(context, true);
	}
	
	public static Phrase GetEditPhrase(Context context)
	{
		return Phrase.GetPhrase(context, Phrase.GetEditPhraseId(context));
	}

	//next phrase
	
	public static void SetNextPhraseId(Context context)
	{
		int currentPhraseId = GetCurrentPhraseId(context);
		
		currentPhraseId ++;
		if (currentPhraseId > PhraseLibrary.getInstance().getPhraseLimit()) currentPhraseId = 1;
			
		SetPhraseId(context, currentPhraseId, false);
	}
		
	public static Phrase GetNextPhrase(Context context)
	{
		int currentPhraseId = GetCurrentPhraseId(context);
		
		currentPhraseId ++;
		if (currentPhraseId > PhraseLibrary.getInstance().getPhraseLimit()) currentPhraseId = 1;
			
		SetPhraseId(context, currentPhraseId, false);
		
		return GetCurrentPhrase(context);
	}

	//previous phrase
	
	public static void SetPreviousPhraseId(Context context)
	{
		int currentPhraseId = GetCurrentPhraseId(context);
		
		currentPhraseId --;
		if (currentPhraseId == 0) currentPhraseId = PhraseLibrary.getInstance().getPhraseLimit();
			
		SetPhraseId(context, currentPhraseId, false);
	}
	
	public static Phrase GetPreviousPhrase(Context context)
	{
		int currentPhraseId = GetCurrentPhraseId(context);
		
		currentPhraseId --;
		if (currentPhraseId == 0) currentPhraseId = PhraseLibrary.getInstance().getPhraseLimit();
			
		SetPhraseId(context, currentPhraseId, false);
		
		return GetCurrentPhrase(context);
	}
	
	//first phrase
	
	public static void SetFirstPhraseId(Context context)
	{
		SetPhraseId(context, 1, false);
	}

	public static Phrase GetFirstPhrase(Context context)
	{
		int currentPhraseId = 1;
		
		SetPhraseId(context, currentPhraseId, false);
		
		return GetCurrentPhrase(context);
	}

	
	public void UpdatePhrase(Context context)
	{
		SharedPreferences sharedPreferences;
    	sharedPreferences = context.getSharedPreferences("SharedPreferences", Context.MODE_WORLD_READABLE);
    	SharedPreferences.Editor editor = sharedPreferences.edit();
		
		editor.putString("TextToDisplay" + _phraseId, _textToDisplay);
		editor.putInt("TextSize" + _phraseId, _textSize.ordinal());
		editor.putInt("TextWidth" + _phraseId, _textWidth.ordinal());
		editor.putInt("TextShade" + _phraseId, _textShade.ordinal());
		editor.putInt("ColourOne" + _phraseId, _colourOne.ordinal());
		editor.putInt("ColourTwo" + _phraseId, _colourTwo.ordinal());
		editor.putInt("ColourThree" + _phraseId, _colourThree.ordinal());
		editor.putInt("BackgroundColour" + _phraseId, _backgroundColour.ordinal());
		editor.putInt("ShadeType" + _phraseId, _shadeType.ordinal());
		editor.putInt("DisplaySpeed" + _phraseId, _displaySpeed.ordinal());
		editor.putInt("ScrollType" + _phraseId, _scrollType.ordinal());
		editor.putInt("FlashType" + _phraseId, _flashType.ordinal());
		
		editor.commit();
	}
	
	public static void SetUpPhrase(Context context, int phraseId, String textToDisplay, TextSizes textSize, TextWidths textWidth, TextShades textShade,
			Colours colourOne, Colours colourTwo, Colours colourThree, Colours backgroundColour,
			ShadeTypes shadeType, DisplaySpeeds displaySpeed, ScrollTypes scrollType, FlashTypes flashType)
	{
		SharedPreferences sharedPreferences;
    	sharedPreferences = context.getSharedPreferences("SharedPreferences", Context.MODE_WORLD_READABLE);
    	SharedPreferences.Editor editor = sharedPreferences.edit();
		
		editor.putString("TextToDisplay" + phraseId, textToDisplay);
		editor.putInt("TextSize" + phraseId, textSize.ordinal());
		editor.putInt("TextWidth" + phraseId, textWidth.ordinal());
		editor.putInt("TextShade" + phraseId, textShade.ordinal());
		editor.putInt("ColourOne" + phraseId, colourOne.ordinal());
		editor.putInt("ColourTwo" + phraseId, colourTwo.ordinal());
		editor.putInt("ColourThree" + phraseId, colourThree.ordinal());
		editor.putInt("BackgroundColour" + phraseId, backgroundColour.ordinal());
		editor.putInt("ShadeType" + phraseId, shadeType.ordinal());
		editor.putInt("DisplaySpeed" + phraseId, displaySpeed.ordinal());
		editor.putInt("ScrollType" + phraseId, scrollType.ordinal());
		editor.putInt("FlashType" + phraseId, flashType.ordinal());
		
		editor.commit();
	}	
}
